/**
 * 全面数据重置脚本
 * 
 * 功能:
 * 1. 清空Neo4j数据库中的所有关系和非User节点
 * 2. 清空MySQL中的关联表数据(保留基础类型和用户数据)
 * 
 * 使用方法:
 * 1. 确保Neo4j和MySQL服务器都在运行
 * 2. 执行 node resetAllData.js
 * 3. 按提示确认清空操作
 */

// 引入需要的模块
const neo4j = require('neo4j-driver');
const { Sequelize } = require('sequelize');
const readline = require('readline');

// 数据库连接配置
// Neo4j配置
const NEO4J_URI = process.env.NEO4J_URI || 'neo4j://139.186.157.99:7687';
const NEO4J_USER = process.env.NEO4J_USER || 'neo4j';
const NEO4J_PASSWORD = process.env.NEO4J_PASSWORD || 'Haoziwoaini1314..'; // 替换为你的实际密码

// MySQL配置
const MYSQL_HOST = process.env.MYSQL_HOST || 'localhost';
const MYSQL_PORT = process.env.MYSQL_PORT || 3306;
const MYSQL_USER = process.env.MYSQL_USER || 'root';
const MYSQL_PASSWORD = process.env.MYSQL_PASSWORD || '123456'; // 替换为你的实际密码
const MYSQL_DATABASE = process.env.MYSQL_DATABASE || 'zstp';

// 创建Neo4j驱动
const neo4jDriver = neo4j.driver(
    NEO4J_URI, 
    neo4j.auth.basic(NEO4J_USER, NEO4J_PASSWORD)
);

// 创建MySQL连接
const sequelize = new Sequelize(MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD, {
    host: MYSQL_HOST,
    port: MYSQL_PORT,
    dialect: 'mysql',
    logging: false
});

// 创建命令行交互接口
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

// 清空Neo4j数据库
async function clearNeo4jDatabase() {
    const session = neo4jDriver.session();
    
    try {
        console.log('【Neo4j】开始清空数据库...');
        
        // 删除所有关系
        console.log('【Neo4j】删除所有关系...');
        await session.run('MATCH ()-[r]-() DELETE r');
        
        // 删除所有非User节点
        console.log('【Neo4j】删除所有非User节点...');
        await session.run('MATCH (n) WHERE NOT n:User DELETE n');
        
        console.log('【Neo4j】数据库已成功清空!');
        return true;
    } catch (error) {
        console.error('【Neo4j】清空数据库时出错:', error);
        return false;
    } finally {
        await session.close();
    }
}

// 清空MySQL关联表数据
async function clearMySQLData() {
    try {
        console.log('【MySQL】开始清空关联表数据...');
        
        // 检查连接
        await sequelize.authenticate();
        console.log('【MySQL】数据库连接成功');
        
        // 清空表数据 (按照依赖顺序删除)
        console.log('【MySQL】删除user_actions表数据');
        await sequelize.query('DELETE FROM user_actions');
        
        console.log('【MySQL】删除goods_tag表数据');
        await sequelize.query('DELETE FROM goods_tag');
        
        console.log('【MySQL】删除related_events表数据 (历史事件)');
        await sequelize.query('DELETE FROM related_events');
        
        console.log('【MySQL】删除related_figures表数据 (历史人物)');
        await sequelize.query('DELETE FROM related_figures');
        
        console.log('【MySQL】删除goods_imgs表数据');
        await sequelize.query('DELETE FROM goods_imgs');
        
        console.log('【MySQL】删除goods表数据');
        await sequelize.query('DELETE FROM goods');
        
        console.log('【MySQL】删除history表数据');
        await sequelize.query('DELETE FROM history');
        
        console.log('【MySQL】关联表数据已成功清空!');
        return true;
    } catch (error) {
        console.error('【MySQL】清空数据时出错:', error);
        return false;
    }
}

// 主函数
async function resetAllData() {
    // 请求用户确认
    rl.question('警告: 此操作将清空所有数据！确认继续吗？(y/n): ', async (answer) => {
        if (answer.toLowerCase() === 'y') {
            console.log('开始重置数据...');
            
            // 清空Neo4j数据
            const neo4jSuccess = await clearNeo4jDatabase();
            
            // 清空MySQL数据
            const mysqlSuccess = await clearMySQLData();
            
            if (neo4jSuccess && mysqlSuccess) {
                console.log('所有数据已成功重置!');
            } else {
                console.log('数据重置过程中出现错误，请检查日志.');
            }
        } else {
            console.log('操作已取消.');
        }
        
        // 关闭连接
        await neo4jDriver.close();
        await sequelize.close();
        rl.close();
    });
}

// 执行重置操作
resetAllData(); 